********************************************************************************
* CREATE ZNTAILS DATASET, USING N DISTS, WITH ZC TAIL PROBABILITIES PER MONTH -- bad programming because really copy and paste of Z program but ok
 
/*
*Run this block if stand alone. If commented out, it is because this will be run from _ricardo_master.do
set more off, perm
clear
graph drop _all
local dir "/Users/rreis/Dropbox/05Shared_folders/HRR/HRR_InflDisaster/Ricardo"  // RR's directory
cd `dir'
local region = "US" 	// set region US or EZ
local updateIteration "Mar2022"
*/

local region `1'
local updateIteration `2'

*Redo monthly choice to make sure it is, and drop empty variables
u "$input/`region'_dist_Z_monthly", clear

gen day = day(date_stata)
gen month = month(date_stata)
gen year = year(date_stata)
sort year month day
    collapse (first) date_stata, by(year month)
    keep date_stata
    merge 1:m date using "$input/`region'_dist_Z_monthly.dta"
    keep if _merge == 3
    drop _merge
*drop I J K L M N O

// Generate 5-year cumulative tails
generate tails_5y = frequency if dist_identifier=="ZN5" 

generate uptails_5y = tails_5y if support>=.04
egen Ntail4_5y = total(uptails_5y), by(date_stata)
replace uptails_5y=. if support<0.05
egen Ntail5_5y = total(uptails_5y), by(date_stata)
drop uptails_5y

generate dotails_5y = tails_5y if support<=0
egen Ntail0_5y = total(dotails_5y), by(date_stata)
replace dotails_5y=. if support>-0.01
egen Ntailm1_5y = total(dotails_5y), by(date_stata)
drop dotails_5y

generate dotails_5y = tails_5y if support<=0.01
egen Ntail1_5y = total(dotails_5y), by(date_stata)
drop dotails_5y

// Generate 5-year densities
g nm1_5y = tails_5y if support==-0.01
g n1_5y = tails_5y if support==0.01
g n0_5y = tails_5y if support==0.0
g n4_5y = tails_5y if support==0.04
g n5_5y = tails_5y if support==0.05

drop tails_5y

// Generate 10-year cumulative tails
generate tails_10y = frequency if dist_identifier=="ZN10" 

generate uptails_10y = tails_10y if support>=.04
egen Ntail4_10y = total(uptails_10y), by(date_stata)
replace uptails_10y=. if support<0.05
egen Ntail5_10y = total(uptails_10y), by(date_stata)
drop uptails_10y

generate dotails_10y = tails_10y if support<=0
egen Ntail0_10y = total(dotails_10y), by(date_stata)
replace dotails_10y=. if support>-0.01
egen Ntailm1_10y = total(dotails_10y), by(date_stata)
drop dotails_10y

generate dotails_10y = tails_10y if support<=0.01
egen Ntail1_10y = total(dotails_10y), by(date_stata)
drop dotails_10y

// Generate 10-year densities
g nm1_10y = tails_10y if support==-0.01
g n0_10y = tails_10y if support==0.0
g n1_10y = tails_10y if support==0.01
g n4_10y = tails_10y if support==0.04
g n5_10y = tails_10y if support==0.05

drop tails_10y

// Collapse and drop all other variables
collapse date Ntail4_5y Ntail5_5y Ntailm1_5y Ntail0_5y Ntail4_10y Ntail5_10y Ntailm1_10y Ntail0_10y n4_5y n4_10y n5_5y n5_10y nm1_5y nm1_10y n0_5y n0_10y  Ntail1_5y Ntail1_10y n1_5y n1_10y , by(date_stata)


// Generate year month variable
g date_ym = mofd(date_stata)
format date_ym %tm

// save it
sleep 100
save "$input/`region'_ZNtails.dta", replace
